package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.constant.MessageConstant;
import com.igeek.collateral.constant.RedisImgConstant;
import com.igeek.collateral.entity.Agency;
import com.igeek.collateral.entity.Member;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IMemberService;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Optional;


@RestController
@RequestMapping("/admin/member")
@Api(tags = "会员管理接口")
@Slf4j
public class MemberController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private IMemberService iMemberService;
    @GetMapping("findPage")
    @ApiOperation("查询会员")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Member> page = iMemberService.findPage(vo);
            if(page!=null){
                return new Result(true , MessageConstant.QUERY_MEMBER_SUCCESS, page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_MEMBER_FAIL);
    }
    @ApiOperation("新增会员")
    @PostMapping("add")
    public Result add(@ApiParam("会员") @RequestBody Member member) {

            boolean flag = iMemberService.save(member);
         if(flag){
           redisTemplate.opsForSet().add(RedisImgConstant.UPLOAD_DB_IMG,member.getImg());
             return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);

        }
        return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询用户信息")
    public Result findById(@PathVariable("id") Long id){
        try {
            Member member = iMemberService.findOneById(id);
            if(member!=null){
                return new Result(true , MessageConstant.QUERY_MEMBER_SUCCESS, member);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_MEMBER_FAIL);
    }
    @PutMapping("update")
    @ApiOperation("编辑会员")
    public Result update(@RequestBody Member member){
        try {
            boolean flag = iMemberService.updateById(member);
            if(flag){
                return new Result(true , MessageConstant.EDIT_AGENCY_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.EDIT_AGENCY_FAIL);
    }
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除会员信息")
    public Result delete (@PathVariable("id") Long id)
    {
        try {
            boolean flag=iMemberService.delete(id);
            if(flag)
            {
                return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
    }
    @PostMapping("upload")
    @ApiOperation("会员头像上传")
    public Result upload(MultipartFile imgFile) throws IOException {
        //返回的是图片名字，拼接上七牛云的域名进行显示
        String pic = iMemberService.uploadByQiniuyun(imgFile);

        //返回的是外链地址，可用于直接显示
        //String pic = setmealService.uploadByAliyun(imgFile);
        if(StringUtils.hasLength(pic)){
            redisTemplate.opsForSet().add(RedisImgConstant.UPLOAD_IMG,pic);
            return new Result(true , MessageConstant.PIC_UPLOAD_SUCCESS, (Object) pic);
        }
        return new Result(false , MessageConstant.PIC_UPLOAD_FAIL);
    }

}
